home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / libcc / datebook.cc < prev    next >
C/C++ Source or Header  |  1997-05-23  |  9KB  |  411 lines

  1. #include "pi-source.h"
  2. #include "pi-datebook.h"
  3.  
  4. #define alarmFlag 64
  5. #define repeatFlag 32
  6. #define noteFlag 16
  7. #define exceptFlag 8
  8. #define descFlag 4
  9.  
  10. appointmentAppInfo_t::appointmentAppInfo_t(void *ai) 
  11.      : appInfo_t(ai) 
  12. {
  13.      unsigned char *ptr = ((unsigned char *) ai) + BASE_APP_INFO_SIZE;
  14.  
  15.      _startOfWeek = get_byte(ptr);
  16. }
  17.  
  18. void *appointmentAppInfo_t::pack(void) 
  19. {
  20.      unsigned char *buffer = new unsigned char [APPOINTMENT_APP_INFO_SIZE];
  21.      
  22.      baseAppInfoPack(buffer);
  23.      
  24.      unsigned char *ptr = buffer + BASE_APP_INFO_SIZE;
  25.      
  26.      set_byte(ptr, _startOfWeek);
  27.      
  28.      return buffer;
  29. }
  30.  
  31. appointment_t::appointment_t(const appointment_t &oldCopy) 
  32. {
  33.      (void) memcpy(this, &oldCopy, sizeof(appointment_t));
  34.  
  35.      // Now fix up any pointers manually
  36.      int len;
  37.  
  38.      if (oldCopy._description) {
  39.       len = strlen(oldCopy._description);
  40.       _description = new char [len + 1];
  41.       (void) strcpy(_description, oldCopy._description);
  42.      }
  43.  
  44.      if (oldCopy._note) {
  45.       len = strlen(oldCopy._note);
  46.       _note = new char [len + 1];
  47.       (void) strcpy(_note, oldCopy._note);
  48.      }
  49.  
  50.      if (oldCopy._repeatEnd) {
  51.       _repeatEnd = new tm;
  52.       (void) memcpy(_repeatEnd, oldCopy._repeatEnd, sizeof(tm));
  53.      }
  54.  
  55.      if (_numExceptions) {
  56.       _exceptions = new tm[_numExceptions];
  57.       (void) memcpy(_exceptions, oldCopy._exceptions,
  58.             _numExceptions * sizeof(tm));
  59.      }
  60. }
  61.  
  62. void appointment_t::unpack(void *buf, int firstTime) 
  63. {
  64.      // If we unpack more than once, we need to free up any old data first
  65.      // so that we don't leak memory
  66.      if (!firstTime) {
  67.       if (_repeatEnd != NULL)
  68.            delete _repeatEnd;
  69.       if (_numExceptions != 0)
  70.            delete [] _exceptions;
  71.       if (_description)
  72.            delete _description;
  73.       if (_note)
  74.            delete _note;
  75.      }
  76.      
  77.      unsigned char *ptr = (unsigned char *) buf;
  78.  
  79.      unsigned short int d;
  80.      
  81.      _begin.tm_hour = get_byte(ptr);
  82.      
  83.      ++ptr;
  84.      _begin.tm_min = get_byte(ptr);
  85.      _begin.tm_sec = 0;
  86.      
  87.      ptr += 3;
  88.      getBufTm(&_begin, ptr, 0);
  89.      
  90.      (void) memcpy(&_end, &_begin, sizeof(tm));
  91.      
  92.      _end.tm_hour = get_byte(((unsigned char *) buf) + 2);
  93.      _end.tm_min = get_byte(((unsigned char *) buf) + 3);
  94.  
  95.      if (get_short(buf) == 0xffff) {
  96.       _begin.tm_hour = 0;
  97.       _begin.tm_min = 0;
  98.       _end.tm_hour = 0;
  99.       _end.tm_min = 0;
  100.       _untimed = 1;
  101.      } else
  102.       _untimed = 0;
  103.  
  104.      mktime(&_end);
  105.  
  106.      ptr += 2;
  107.      
  108.      int flags = get_byte(ptr);
  109.  
  110.      ptr += 2;
  111.  
  112.      if (flags & alarmFlag) {
  113.       _hasAlarm = 1;
  114.       _advance = get_byte(ptr);
  115.  
  116.       ++ptr;
  117.       _advanceUnits = (alarmUnits_t) get_byte(ptr);
  118.  
  119.       ++ptr;
  120.      } else
  121.       _hasAlarm = 0;
  122.  
  123.      if (flags & repeatFlag) {
  124.       _repeatType = (repeatType_t) get_byte(ptr);
  125.  
  126.       ptr += 2;
  127.       d = (unsigned short int) get_short(ptr);
  128.  
  129.            
  130.       if (d != 0xffff) {
  131.            _repeatEnd = new tm;
  132.            _repeatEnd->tm_year = (d >> 9) + 4;
  133.            _repeatEnd->tm_mon = ((d >> 5) & 15) - 1;
  134.            _repeatEnd->tm_mday = d & 31;
  135.            _repeatEnd->tm_min = 0;
  136.            _repeatEnd->tm_hour = 0;
  137.            _repeatEnd->tm_sec = 0;
  138.            _repeatEnd->tm_isdst = -1;
  139.            mktime(_repeatEnd);
  140.       } else
  141.            _repeatEnd = NULL;
  142.  
  143.       ptr += 2;
  144.       _repeatFreq = get_byte(ptr);
  145.  
  146.       ++ptr;
  147.       _repeatOn = get_byte(ptr);
  148.  
  149.       ++ptr;
  150.       _repeatWeekstart = get_byte(ptr);
  151.  
  152.       ptr += 2;
  153.      } else {
  154.       _repeatType = none;
  155.       _repeatEnd = NULL;
  156.      }
  157.  
  158.      if (flags & exceptFlag) {
  159.       _numExceptions = get_short(ptr);
  160.  
  161.       ptr += 2;
  162.       _exceptions = new tm [_numExceptions];
  163.  
  164.       for (int i = 0; i < _numExceptions; i++, ptr += 2)
  165.            getBufTm(&_exceptions[i], ptr, 0);
  166.      } else {
  167.       _numExceptions = 0;
  168.       _exceptions = NULL;
  169.      }
  170.      
  171.      int len;
  172.  
  173.      if (flags & descFlag) {
  174.       len = strlen((const char *) ptr) + 1;
  175.       _description = new char [len];
  176.       (void) strcpy((char *) _description, (const char *) ptr);
  177.       ptr += len;
  178.      } else
  179.       _description = NULL;
  180.      
  181.      if (flags & noteFlag) {
  182.       len = strlen((const char *) ptr) + 1;
  183.       _note = new char [len];
  184.       (void) strcpy((char *) _note, (const char *) ptr);
  185.       ptr += len;
  186.      } else
  187.       _note = NULL;
  188.  
  189.      _next = NULL;
  190. }
  191.  
  192. void *appointment_t::internalPack(unsigned char *buf) 
  193. {
  194.      unsigned char *ptr = buf;
  195.      
  196.      set_byte(ptr, _begin.tm_hour);
  197.      set_byte(++ptr, _begin.tm_min);
  198.      set_byte(++ptr, _end.tm_hour);
  199.      set_byte(++ptr, _end.tm_min);
  200.  
  201.      set_short(++ptr, ((_begin.tm_year - 4) << 9) | ((_begin.tm_mon  + 1) << 5) | _begin.tm_mday);
  202.      
  203.      if (_untimed)
  204.       set_long(ptr, 0xffffffff);
  205.  
  206.      ptr += 4;            // Now at buf + 8
  207.  
  208.      int iflags = 0;
  209.      
  210.      if (_hasAlarm) {
  211.       iflags |= alarmFlag;
  212.       set_byte(ptr, _advance);
  213.       set_byte(++ptr, _advanceUnits);
  214.       ++ptr;
  215.      }
  216.  
  217.      if (_repeatType != none) {
  218.       iflags |= repeatFlag;
  219.       set_byte(ptr, _repeatType);
  220.       set_byte(++ptr, 0);
  221.  
  222.       ++ptr;
  223.  
  224.       if (_repeatEnd)
  225.            setBufTm(ptr, _repeatEnd);
  226.       else
  227.            set_short(ptr, 0xffff);
  228.       
  229.       ptr += 2;
  230.  
  231.       set_byte(ptr, _repeatFreq);
  232.       set_byte(++ptr, _repeatOn);
  233.       set_byte(++ptr, _repeatWeekstart);
  234.       set_byte(++ptr, 0);
  235.  
  236.       ++ptr;
  237.      }
  238.      
  239.      if (_exceptions) {
  240.       iflags |= exceptFlag;
  241.  
  242.       set_short(ptr, _numExceptions);
  243.       ptr += 2;
  244.  
  245.       for (int i = 0; i < _numExceptions; i++, ptr += 2)
  246.            setBufTm(ptr, &_exceptions[i]);
  247.      }
  248.  
  249.      if (_description) {
  250.       iflags |= descFlag;
  251.  
  252.       (void) strcpy((char *) ptr, _description);
  253.       ptr += strlen(_description) + 1;
  254.      }
  255.  
  256.      set_byte(buf + 6, iflags);
  257.  
  258.      return buf;
  259. }
  260.  
  261. void *appointment_t::pack(int *len) 
  262. {
  263.      *len = 8;
  264.  
  265.      if (_hasAlarm)
  266.       *len += 2;
  267.       
  268.      if (_repeatType != none)
  269.       *len += 8;
  270.  
  271.      if (_exceptions)
  272.       *len += 2 + (2 * _numExceptions);
  273.      
  274.      if (_description)
  275.       *len += strlen(_description) + 1;
  276.  
  277.      unsigned char *ret = new unsigned char [*len];
  278.      return internalPack(ret);
  279. }
  280.  
  281. void *appointment_t::pack(void *buf, int *len) 
  282. {
  283.      int totalLength = 8;
  284.  
  285.      if (_hasAlarm)
  286.       totalLength += 2;
  287.       
  288.      if (_repeatType != none)
  289.       totalLength += 8;
  290.  
  291.      if (_exceptions)
  292.       totalLength += 2 + (2 * _numExceptions);
  293.      
  294.      if (_description)
  295.       totalLength += strlen(_description) + 1;
  296.  
  297.      if (*len < totalLength)
  298.       return NULL;
  299.  
  300.      *len = totalLength;
  301.  
  302.      return internalPack((unsigned char *) buf);
  303. }
  304.  
  305. appointment_t::~appointment_t(void) 
  306. {
  307.      if (_repeatEnd)
  308.       delete _repeatEnd;
  309.      if (_exceptions)
  310.       delete _exceptions;
  311.      if (_note)
  312.       delete _note;
  313.      if (_description)
  314.       delete _description;
  315. }
  316.  
  317. int appointment_t::operator<(const appointment_t &right)
  318. {
  319.      tm a, b;
  320.  
  321.      (void) memcpy(&a, &_begin, sizeof(tm));
  322.      (void) memcpy(&b, &(right._begin), sizeof(tm));
  323.  
  324.      return mktime(&a) < mktime(&b);
  325. }
  326.  
  327. int appointment_t::operator>(const appointment_t &right)
  328. {
  329.      tm a, b;
  330.  
  331.      (void) memcpy(&a, &_begin, sizeof(tm));
  332.      (void) memcpy(&b, &(right._begin), sizeof(tm));
  333.  
  334.      return mktime(&a) > mktime(&b);
  335. }
  336.  
  337. // I doubt this works properly.  I haven't done any testing yet.
  338. int appointment_t::operator==(const appointment_t &right) 
  339. {
  340. #if 1
  341.      return memcmp(this, &right, sizeof(appointment_t));
  342. #else
  343.      tm a, b;
  344.  
  345.      (void) memcpy(&a, &_begin, sizeof(tm));
  346.      (void) memcpy(&b, &(right._begin), sizeof(tm));
  347.  
  348.      if (mktime(&a) != mktime(&b)) 
  349.     return 0;
  350.  
  351.      (void) memcpy(&a, &_end, sizeof(tm));
  352.      (void) memcpy(&b, &(right._end), sizeof(tm));
  353.  
  354.      if (mktime(&a) != mktime(&b)) 
  355.     return 0;
  356.      
  357.      if (strcmp(_description, right._description))
  358.     return 0;
  359.  
  360.      if (strcmp(_note, right._note))
  361.     return 0;
  362.  
  363.      if (_hasAlarm) {
  364.     if (right._hasAlarm == 0)
  365.         return 0;
  366.  
  367.     if (_advance != right._advance || _advanceUnits != right._advanceUnits)
  368.         return 0;
  369.      }
  370.  
  371.      if (_repeatType != right._repeatType)
  372.     return 0;
  373.  
  374.      if (_numExceptions != right._numExceptions)
  375.     return 0;
  376.  
  377.      return 0;
  378. #endif
  379. }
  380.  
  381. appointmentList_t::~appointmentList_t() 
  382. {
  383.      appointment_t *next;
  384.      
  385.      for (appointment_t *head = _head; head != NULL; head = next) {
  386.       next = head->_next;
  387.       delete head;
  388.      }
  389. }
  390.  
  391. // We can't just point to the data, as it might be deleted.  Make a copy
  392. void appointmentList_t::merge(appointment_t &appointment) 
  393. {
  394.      appointment._next = _head;
  395.      _head = new appointment_t(appointment);
  396. }
  397.  
  398. // We can't just point to the data in the list, as it might get deleted on
  399. // us. We need to make a real copy
  400. void appointmentList_t::merge(appointmentList_t &list) 
  401. {
  402.      appointment_t *newguy;
  403.  
  404.      for (appointment_t *ptr = list._head; ptr != NULL; ptr = ptr->_next) {
  405.           newguy = new appointment_t(ptr);
  406.           newguy->_next = _head;
  407.           _head = newguy;
  408.      }
  409. }
  410.      
  411.